package com.cosytek.farmmanager;

import android.content.Context;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

/**
 * Created by Happy on 2015/3/25.
 */
public class ServerError {
    /**
     * error code.
     */
    public static final int ERR_SUCCEED = 0;
    /**
     * error code.
     */
    public static final int ERR_DATABASE = 1;
    /**
     * error code.
     */
    public static final int ERR_TOKEN_EXPIRED = 2;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NOT_EXIST = 3;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NOT_MATCH = 4;
    /**
     * error code.
     */
    public static final int ERR_ACCOUNT_NEED = 5;
    /**
     * error code.
     */
    public static final int ERR_PASSWORD_NEED = 6;
    /**
     * error code.
     */
    public static final int ERR_ENCODE = 7;
    /**
     * error code.
     */
    public static final int ERR_DECODE = 8;

    /**
     * cancel error.
     */
    public static final int ERR_TASK_NOT_CANCEL_ABLE = 32;
    /**
     * task has start.
     */
    public static final int ERR_TASK_ALREADY_START = 33;
    /**
     * not running.
     */
    public static final int ERR_TASK_NOT_RUNNING = 34;
    /**
     * not belong to account.
     */
    public static final int ERR_TASK_NOT_BELONG2_ACCOUNT = 35;
    /**
     * task not exist.
     */
    public static final int ERR_TASK_NOT_EXIST = 36;
    /**
     * controller not valid.
     */
    public static final int ERR_TASK_CONTROLLER_NOT_VALID = 37;
    /**
     * controller is busy.
     */
    public static final int ERR_TASK_CONTROLLER_BUSY = 38;
    /**
     * task send command 2 controller failed.
     */
    public static final int ERR_TASK_SEND2_CONTROLLER = 39;
    /**
     * task timeout.
     */
    public static final int ERR_TASK_TIMEOUT = 40;

    /**
     * Get error string.
     *
     * @param err
     *            Error code string.
     * @return Error String.
     */
    public static String getErrString(final int err) {
        String errStr = "";
        Context context = FarmManagerApp.getAppContext();
        switch(err) {
            case ERR_SUCCEED:
                errStr = context.getString(R.string.ERR_SUCCEED);
                break;
            case ERR_DATABASE:
                errStr = context.getString(R.string.ERR_DATABASE);
                break;
            case ERR_TOKEN_EXPIRED:
                errStr = context.getString(R.string.ERR_TOKEN_EXPIRED);
                break;
            case ERR_ACCOUNT_NOT_EXIST:
                errStr = context.getString(R.string.ERR_ACCOUNT_NOT_EXIST);
                break;
            case ERR_PASSWORD_NOT_MATCH:
                errStr = context.getString(R.string.ERR_PASSWORD_NOT_MATCH);
                break;
            case ERR_ACCOUNT_NEED:
                errStr = context.getString(R.string.ERR_ACCOUNT_NEED);
                break;
            case ERR_PASSWORD_NEED:
                errStr = context.getString(R.string.ERR_PASSWORD_NEED);
                break;
            case ERR_ENCODE:
                errStr = context.getString(R.string.ERR_ENCODE);
                break;
            case ERR_DECODE:
                errStr = context.getString(R.string.ERR_DECODE);
                break;
            case ERR_TASK_NOT_CANCEL_ABLE:
                errStr = context.getString(R.string.ERR_TASK_NOT_CANCEL_ABLE);
                break;
            case ERR_TASK_ALREADY_START:
                errStr = context.getString(R.string.ERR_TASK_ALREADY_START);
                break;
            case ERR_TASK_NOT_RUNNING:
                errStr = context.getString(R.string.ERR_TASK_NOT_RUNNING);
                break;
            case ERR_TASK_NOT_BELONG2_ACCOUNT:
                errStr = context.getString(R.string.ERR_TASK_NOT_BELONG2_ACCOUNT);
                break;
            case ERR_TASK_NOT_EXIST:
                errStr = context.getString(R.string.ERR_TASK_NOT_EXIST);
                break;
            case ERR_TASK_CONTROLLER_NOT_VALID:
                errStr = context.getString(R.string.ERR_TASK_CONTROLLER_NOT_VALID);
                break;
            case ERR_TASK_CONTROLLER_BUSY:
                errStr = context.getString(R.string.ERR_TASK_CONTROLLER_BUSY);
                break;
            case ERR_TASK_SEND2_CONTROLLER:
                errStr = context.getString(R.string.ERR_TASK_SEND2_CONTROLLER);
                break;
            case ERR_TASK_TIMEOUT:
                errStr = context.getString(R.string.ERR_TASK_TIMEOUT);
                break;
            default:
                errStr = context.getString(R.string.ERR_UNKNOWN);
                break;
        }
        return errStr;
    }

    /**
     * construct.
     * @param code code
     * @param msg message
     */
    public ServerError(int code, String msg) {
        mCode = code;
        mMsg = msg;
    }

    public static ServerError genericNetworkError() {
        Context c = FarmManagerApp.getAppContext();
        String errStr = c.getString(R.string.ERR_NETWORK);
        return new ServerError(-1, errStr);
    }

    /**
     * code.
     */
    private final int mCode;
    /**
     * message.
     */
    private final String mMsg;
    /**
     * get code.
     * @return code
     */
    public int getCode() {
        return mCode;
    }
    /**
     * get message.
     * @return message
     */
    public String getMsg() {
        return mMsg;
    }

    public static ServerError parseServerError(HttpResponse resp) {
        Context c = FarmManagerApp.getAppContext();
        if (resp.getStatusLine().getStatusCode() != 200) {
            return new ServerError(-1, c.getString(R.string.ERR_HTTP_REQUEST));
        }

        Header header = resp.getFirstHeader("error");
        int code = 0;
        if (header != null) {
            code = Integer.parseInt(header.getValue());
        }

        if (code == 0) {
            return null;
        }

        String msg = null;
        header = resp.getFirstHeader("errmsg");
        if (header != null) {
            msg = header.getValue();
        }

        if (msg == null) {
            msg = getErrString(code);
        }

        return new ServerError(code, msg);
    }

    public static boolean hasError(HttpResponse resp) {
        if (resp.getStatusLine() != null
                && 200 != resp.getStatusLine().getStatusCode()) {
            return true;
        }

        Header header = resp.getFirstHeader("error");
        int code = 0;
        if (header != null) {
            return true;
        }
        return false;
    }

}
